package com.rms.web.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.rms.domain.CompanyManager;
import com.rms.domain.Hr;
import com.rms.domain.Job;
import com.rms.utils.servlet.BaseServlet;

/**
 * 公司管理员servlet 功能:添加HR，撤销HR，添加工作机会，分配简历给HR，登录，退出，注册
 * 
 * @author Zheng weiyu
 * @date 2019年7月29日
 */
@WebServlet("/CompanyManagerServlet")
public class CompanyManagerServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private static final String SESSION_NAME = "companyManager";
	/**
	 * 封装表单数据
	 * 
	 * @param req
	 * @return
	 * @author Zheng weiyu 2019年7月29日
	 */
	private CompanyManager toBean(HttpServletRequest req) {
		CompanyManager manager = new CompanyManager();
		try {
			BeanUtils.populate(manager, req.getParameterMap());
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return manager;
	}


	public void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		/**
		 * 1.封装表单数据 2.调用service的regist方法 3.注册成功转发到login.jsp 注册失败转发到regist.jsp
		 */
		// 封装表单数据
		CompanyManager manager = toBean(req);
		// 调用service的login方法
		try {
			companyManagerService.regist(manager);
			// 执行成功,跳转到main.jsp
			req.setAttribute("message", "注册成功!");
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		} catch (Exception e) {
			// 登录失败，保存错误信息并跳转到login.jsp
			e.printStackTrace();
			req.setAttribute("message", "注册失败!" + e.getMessage());
			req.getRequestDispatcher("regist.jsp").forward(req, resp);
		}
	}

	public void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		/**
		 * 1.移除session域的user 2.成功转发到login.jsp
		 */
		try {
			req.getSession().removeAttribute(SESSION_NAME);
			req.setAttribute("message", "退出成功!");
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void addHR(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		/**
		 * 1.封装表单数据
		 * 2.调用companyService的addHR方法
		 * 3.成功-》转发
		 *   失败-》转发
		 * */
		//封装数据
		Hr hr = new Hr();
		try {
			BeanUtils.populate(hr, req.getParameterMap());
			hr.setRoleId(2);
			hr.setCompanyId(((CompanyManager)(req.getSession().getAttribute("companyManager"))).getCompanyId());
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//调用service的addHR方法
		try {
			companyManagerService.addHR(hr);
			CompanyManager manager = (CompanyManager)req.getSession().getAttribute(SESSION_NAME);
			//进行数据更新
			List<Hr> list = companyManagerService.findHRByManagerId(manager.getId());
			manager.getCompany().setHrs(list);
			req.setAttribute("message", "添加成功!");
		}catch(Exception e) {
			e.printStackTrace();
			req.setAttribute("message", e.getMessage());
		}
		req.getRequestDispatcher("ToManagerMainPageServlet").forward(req, resp);
	}

	public void deleteHR(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int hr_id = Integer.parseInt(req.getParameter("hr_id"));
		try {
			companyManagerService.deleteHR(hr_id);
			CompanyManager manager = (CompanyManager)req.getSession().getAttribute(SESSION_NAME);
			List<Hr> list = companyManagerService.findHRByManagerId(manager.getId());
			manager.getCompany().setHrs(list);
			req.setAttribute("message", "删除成功!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			req.setAttribute("message", "删除失败!"+e.getMessage());
	
		}
		req.getRequestDispatcher("ToManagerMainPageServlet").forward(req, resp);
	}

	public void sendResumeToHR(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		/**
		 * 得到前端传入的数据
		 * 
		 * */
		//单选框传入
		int hr_id = Integer.parseInt(req.getParameter("hr_id"));
		//复选框传入,需要转成int数组(该数组为resumeJobId数组)
		String[] selectResumeIndex = req.getParameterValues("selectedJob");
		try {
			companyManagerService.sendResumeToHR(hr_id,selectResumeIndex);
			req.setAttribute("message", "分配成功!");
			req.getRequestDispatcher("ToManagerMainPageServlet").forward(req, resp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			req.setAttribute("message","分配失败!"+ e.getMessage());
			req.getRequestDispatcher("ToManagerMainPageServlet").forward(req, resp);
	
		}
	}

	public void addJob(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//封装数据
		Job job = new Job();
		try {
			BeanUtils.populate(job, req.getParameterMap());
			job.setCompanyId(((CompanyManager)req.getSession().getAttribute("companyManager")).getCompanyId());
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//调用service的addJob
		try {
			companyManagerService.addJob(job);
			//更新数据
			CompanyManager manager = (CompanyManager)req.getSession().getAttribute(SESSION_NAME);
			List<Job> list = companyManagerService.findJobByManagerId(manager.getId());
			manager.getCompany().setJobs(list);
			req.setAttribute("message", "添加工作成功!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			req.setAttribute("message","添加工作失败!"+ e.getMessage());
		}
		req.getRequestDispatcher("ToManagerMainPageServlet").forward(req, resp);
	}
	
	public void deleteJob(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//删除job
		int job_id = Integer.parseInt(req.getParameter("job_id"));
		try {
			companyManagerService.deleteJob(job_id);
			CompanyManager manager = (CompanyManager)req.getSession().getAttribute(SESSION_NAME);
			List<Job> list = companyManagerService.findJobByManagerId(manager.getId());
			manager.getCompany().setJobs(list);
			req.setAttribute("message", "删除成功");
		} catch (Exception e) {
			req.setAttribute("message", "删除失败");
			e.printStackTrace();
		}
		req.getRequestDispatcher("ToAllRecruitmentPageServlet").forward(req, resp);
	
	}

}

